<?php
namespace Forxx\Api;

use \Lavender\Core;
use Golo\InnerHelper;

class Token
{
    /***
     * @param $uid
     * @param $token
     * @param $appid
     * @param bool $bol_quick 是否立即同步
     * @return bool
     */
    public static function set_token($uid,$token,$appid,$bol_quick=false)
	{
		$appid_conf = Core::get_config('const','appid');
		if(in_array($appid,$appid_conf)){
			$appid = 'app1';
		}
		$redis_data = array('token'=>$token,'updated'=>time());
		$dao_token=Dao\TokenRedisTable::instance();
        $dao_token->bol_quick = $bol_quick;
        $dao_token->hset($uid,$appid,$redis_data);
		return true;
	}
	
	/**
	 * 设置用户token值,同时在用户所属区域设置
	 *
	 * @access public
	 * @package \Golo\Api\User
	 * @param array $route
	 * @param array $post_data
	 *
	 * @return mixed 设置成功返回true，失败返回false
	 */
	public static function set_user_token($route, $uid, $token, $appid)
	{
		self::set_token($uid,$token,$appid);
		$post_param['token'] = $token;
		$post_param['uid'] = $uid;
		$post_param['app_id'] = $appid;
		$r = InnerHelper::rpc_call($route['url_inner'], 'token.set_token', array(), $post_param);
		$data = json_decode($r, true);
		if($data['code'] == 0){
			return true;
		}
		return false;
	}

	public static function get_token($uid,$app_id)
	{
		$appid = Core::get_config('const','appid');
		// ios、android、mycar的token前缀都为app1
		if(in_array($app_id,$appid)){
			$app_id = 'app1';
		}
		$redis_data = Dao\TokenRedisTable::instance()->hget($uid,$app_id);
		return $redis_data['token'];
	}

}